import { getVideoList, getVideoInfo, deleteVideo, bulkDeleteVideo } from '@/services/video';

export default {
    namespace: 'video',

    state: {
        videoInfo: {},
        videoList: {},
    },

    effects: {
        *fetchVideoList({ payload, callback }, { call, put }) {
            const response = yield call(getVideoList, payload);
            yield put({
                type: 'videoList',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *fetchVideoInfo({ payload, callback }, { call, put }) {
            const response = yield call(getVideoInfo, payload);
            yield put({
                type: 'videoInfo',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *delete({ payload, callback }, { call, put }) {
            const response = yield call(deleteVideo, payload);
            if (response && callback) {
                callback(response);
            }
        },
        *bulkDelete({ payload, callback }, { call, put }) {
            const response = yield call(bulkDeleteVideo, payload);
            if (response && callback) {
                callback(response);
            }
        },
    },

    reducers: {
        videoList(state, action) {
            let pagination = {};
            pagination.total = action.payload.data.total;
            pagination.pageSize = action.payload.data.perPage;
            pagination.current = action.payload.data.currentPage;
            pagination.showTotal = (total, range) => {
                return `共 ${total} 条记录 当前显示: ${range[0]}-${range[1]}`;
            }
            return {
                ...state,
                videoList: {
                    list: action.payload.data.data,
                    pagination: pagination
                }
            };
        },
        videoInfo(state, action) {
            return {
                ...state,
                videoInfo: action.payload.data.data
            }
        }
    },
};
